<template>
  <t-dialog
    header="修改链"
    :visible.sync="showDialog"
    @confirm="saveForm"
    width="70%"
    :cancelBtn="null"
    :confirmBtn="null"
  >
<t-card title="链类型配置" :bordered="false" class="info-block">
  <t-descriptions>
    <t-descriptions-item label="配置名称" >{{ chain_type_config_Data.chain_config_name }}</t-descriptions-item>
    <t-descriptions-item label="账户模式">{{ auth_type_Options.find(item => item.value === chain_type_config_Data.auth_type)?.label }}</t-descriptions-item>
    <t-descriptions-item label="加密算法">{{ hash_type_Options.find(item => item.value === chain_type_config_Data.hash_type)?.label }}</t-descriptions-item>
    <t-descriptions-item label="共识算法">{{ consensus_type_Options.find(item => item.value === chain_type_config_Data.consensus_type)?.label }}</t-descriptions-item>
    <!-- <t-descriptions-item label="gas开启状态">{{ consensus_type_Options.find(item => item.value === chainconfigData.consensus_type)?.label }}</t-descriptions-item> -->
    <t-descriptions-item label="gas开启状态">
      <t-switch size="large"  :disabled="true" v-model="chain_type_config_Data.gas_enable"></t-switch>
    </t-descriptions-item>
  </t-descriptions>
</t-card>
<t-card title="链性能配置" :bordered="false" class="info-block">
  <t-descriptions title="区块配置" class="sub-descriptions">
    <t-descriptions-item label="区块容量">{{ chain_performance_config_Data.block_tx_capacity }}</t-descriptions-item>
    <t-descriptions-item label="区块大小">{{ chain_performance_config_Data.block_size }}</t-descriptions-item>
    <t-descriptions-item label="出块间隔">{{ chain_performance_config_Data.block_interval }}</t-descriptions-item>
  </t-descriptions>
  <t-descriptions title="交易池配置" class="sub-descriptions" >
    <t-descriptions-item label="交易池类型">{{ chain_performance_config_Data.pool_type }}</t-descriptions-item>
    <t-descriptions-item label="交易池容量">{{ chain_performance_config_Data.max_txpool_size }}</t-descriptions-item>
    <t-descriptions-item label="批次容量">{{ chain_performance_config_Data.batch_max_size }}</t-descriptions-item>
    <t-descriptions-item label="批次间隔">{{ chain_performance_config_Data.batch_create_timeout }}</t-descriptions-item>
  </t-descriptions>
</t-card>
<t-card title="自定义参数" :bordered="false" class="info-block">
  <template v-for="(item, index) in chain_custom_parameters_Data">
    <t-descriptions :key="index">
      <t-descriptions-item label="操作">{{ item.operation }}</t-descriptions-item>
      <t-descriptions-item label="配置文件名">{{ item.config_file_name }}</t-descriptions-item>
      <t-descriptions-item label="配置值">{{ item.config_value }}</t-descriptions-item>
    </t-descriptions>
  </template>
</t-card>
  </t-dialog>
</template>
<script>
import { CheckCircleFilledIcon } from "tdesign-icons-vue";
import { prefix } from "@/config/global";
import { updateSceneConfig } from "@/api/requestApi";
import { auth_type_Options, hash_type_Options, consensus_type_Options } from '@/pages/performance/options.js';


export default {
  name: "FormStep",
  components: {
    CheckCircleFilledIcon,
  },
  props: {
    showChildDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      visible: false,
      checked: true,
      prefix,
      chain_type_config_Data: {},
      chain_performance_config_Data: {},
      chain_custom_parameters_Data: { list: [] },
      sceneData: {},

      requestData: {},
      formData3: { list: [] },
      auth_type_Options: auth_type_Options,
      hash_type_Options: hash_type_Options,
      consensus_type_Options: consensus_type_Options,
      client_type_Options: [
        { label: 'cmc', value: 0 },
        { label: 'go-sdk', value: 1 },
        { label: 'java-sdk', value: 2 },
      ],
      contract_type_Options: [
        { label: '系统合约', value: 0 },
        { label: '用户合约', value: 1 },
      ],
      contract_name_Options: [
        { label: 'T', value: 'T' },
        { label: 'docker-cut', value: 'docker_cut' },
        { label: 'docker-fact', value: 'fact' },
        { label: 'docker-evidence', value: 'evidence' },

      ],
      method_name_Options: [
        { label: 'P', value: 'P' },
        { label: 'save', value: 'save' },
        { label: 'fact', value: 'fact' },
      ],
      param_size_Options: [
        { label: '200byte', value: '200byte' },
        // { label: '500byte', value: '500byte' },
        { label: '5k', value: '5k' },
        { label: '500k', value: '500k' },
      ],
      
      rules: {
        name: [{ required: true, message: '请输入场景名称', type: 'error' }],
        client_type: [{ required: true, message: '请选择客户端类型', type: 'error' }],
        contract_type: [{ required: true, message: '请选择合约类型', type: 'error' }],
        contract_name: [{ required: true, message: '请选择合约名', type: 'error' }],
        contract_method: [{ required: true, message: '请选择方法名', type: 'error' }],
        param_size: [{ required: true, message: '请选择参数大小', type: 'error' }],
        threads: [{ required: true, message: '请输入并发数', type: 'error' }],
        runtime: [{ required: true, message: '请输入运行时长', type: 'error' }],
      },
      activeForm: 0,
    };
  },
  computed: {
    showDialog: {
      get() {
        return this.showChildDialog;
      },
      set(newValue) {
        this.$emit("update:showChildDialog", newValue); // 触发更新事件，父组件的showDialogAdd会自动更新
      },
    },
    amount() {
      if (this.sceneData.chain_config_name === "1") {
        return "565421";
      }
      if (this.sceneData.chain_config_name === "2") {
        return "278821";
      }
      if (this.sceneData.chain_config_name === "3") {
        return "109824";
      }
      return "--";
    },
  },
  methods: {
    init(data) {
      console.log("task_info_来自父组件的", data);

      this.data = data;
      console.log("task_info_来自父组件的chain_config_details", data.chain_config_details);

      const INITIAL_CHAIN_TYPE_CONFIG_DATA = {
        hash_type: data.chain_config_details.chain_type_config.hash_type,
        gas_enable: data.chain_config_details.chain_type_config.gas_enable ===1,
        auth_type: data.chain_config_details.chain_type_config.auth_type,
        chain_config_name: data.chain_config_details.chain_type_config.chain_config_name,
        consensus_type: data.chain_config_details.chain_type_config.consensus_type,
      };
      this.chain_type_config_Data = { ...INITIAL_CHAIN_TYPE_CONFIG_DATA };
      const INITIAL_CHAIN_PERFORMANCE_CONFIG_DATA = {
        block_tx_capacity: data.chain_config_details.chain_performance_config.block_tx_capacity === -1 ? '' : data.chain_config_details.chain_performance_config.block_tx_capacity,
        block_size: data.chain_config_details.chain_performance_config.block_size === -1 ? '' : data.chain_config_details.chain_performance_config.block_size,
        block_interval: data.chain_config_details.chain_performance_config.block_interval === -1 ? '' : data.chain_config_details.chain_performance_config.block_interval,
        max_txpool_size: data.chain_config_details.chain_performance_config.max_txpool_size === -1 ? '' : data.chain_config_details.chain_performance_config.max_txpool_size,
        batch_max_size: data.chain_config_details.chain_performance_config.batch_max_size === -1 ? '' : data.chain_config_details.chain_performance_config.batch_max_size,
        batch_create_timeout: data.chain_config_details.chain_performance_config.batch_create_timeout === -1 ? '' : data.chain_config_details.chain_performance_config.batch_create_timeout,
      };
      this.chain_performance_config_Data = { ...INITIAL_CHAIN_PERFORMANCE_CONFIG_DATA };
      const INITIAL_CHAIN_CUSTOM_PARAMETERS_DATA = data.chain_config_details.chain_custom_parameters.map(item => ({
        operation: item.operation,
        config_file_name: item.config_file_name,
        config_value: item.config_value,
      }));
      this.chain_custom_parameters_Data = [...INITIAL_CHAIN_CUSTOM_PARAMETERS_DATA];
    },

    onSubmit4() {
      this.$router.replace({ path: "/detail/advanced" });
    },
    onReset4() {
      this.activeForm = 0;
    },
    onChange(val) {
      console.log(val);
    },
    // 更新配置
    saveForm() {
      console.log("更新场景配置this_sceneData",this.sceneData);
      const params = {
        request_config: {
          request_name: this.sceneData.scene_name,
          client_type: this.requestData.client_type,
          contract_type: this.requestData.contract_type,
          contract_name: this.requestData.contract_name,
          contract_method: this.requestData.contract_method,
          param_size: this.requestData.param_size,
        },
        scene_config: {
          scene_name: this.sceneData.scene_name,
          threads: this.sceneData.threads,
          runtime: this.sceneData.runtime,
        },
        
      };
      console.log("更新场景配置");
      console.log(params);
      this.$request
        .post(updateSceneConfig(this.data.id), params)
        // .post(updateChainConfig(), params)
        .then((res) => {
          console.log("------",res)
          if (res.data.code === "200") {
            this.$message.success("保存成功");
            this.showDialog = false;
            this.$router.push("/performance/chain");
          } else {
            this.$message.fail(res.data);
          }
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style lang="less" scoped>
@import "../index";
.sub-descriptions {
  margin-left: 20px;
}
.descriptions-item {
  padding-top: 8px;
  padding-bottom: 8px;
}
</style>

